﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.Presentation.ProfileManagement
{
    public partial class ListContractor : System.Web.UI.Page
    {
        private ContractorEntity _contractor;
        private string user;
        private int roleId;
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();
            if (Session["RoleId"] == null || Session["UserName"]==null)
            {
                return;
            }
            if(!IsPostBack)
            {
                BindData();                
            }
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            ButtonSearchClick(null, null);
            ListViewContractor.DataBind();
        }
        private void BindData()
        {
            roleId = int.Parse(Session["RoleId"].ToString(), CultureInfo.InvariantCulture);
            user= Session["UserName"].ToString();

            if(roleId==1)
            {
                ListViewContractor.DataSource = ContractorProfileLogic.SelectData();
            }
            else if(roleId==3)
            {
                ListViewContractor.DataSource = ContractorProfileLogic.GetContractorByClient(user);
            }
            else if(roleId==2)
            {
                Response.Redirect("ViewContractor.aspx");
            }
            else if(roleId==4)
            {
                ListViewContractor.DataSource = ContractorProfileLogic.GetContractorByConsultant(user);
            }

            ListViewContractor.DataBind();
        }

        // Check user's permissions
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int)FunctionList.ListContractor;

            if (Page.Master != null)
            {
                var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");
                AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
            }
        }
        /* or we get "raised event ItemDeleting which wasn't handled" */
        protected void ListViewContractorItemDeleting(object sender, ListViewDeleteEventArgs e)
        { }
        protected void ListViewContractorItemCommand(object sender, CommandEventArgs e)
        {
            roleId = int.Parse(Session["RoleId"].ToString(), CultureInfo.InvariantCulture);
            if (roleId != 1)
            {
                labelResult.Text = "Your permission is not allowed to use the function.";
                return;
            }
            if (e.CommandName == "Delete")
            {
                var id = int.Parse(e.CommandArgument.ToString(),CultureInfo.InvariantCulture);

                _contractor = new ContractorEntity { ContractorId = id };

                ContractorProfileLogic.DeleteContractor(_contractor);

                BindData();
            }
        }

        /// <summary>
        /// Filter search contractor
        /// </summary>
        protected void FilterContractor()
        {
            roleId = int.Parse(Session["RoleId"].ToString(), CultureInfo.InvariantCulture);
            user = Session["UserName"].ToString();
            var fullName = textFullName.Text;
            var email= textEmail.Text;

            if(roleId==1)
            {
                var query = ContractorProfileLogic.SearchContractor(fullName, email);
                if(query==null)
                {
                    return;
                }
                ListViewContractor.DataSource = query;                
            }
            else if (roleId==3)
            {
                var query = ContractorProfileLogic.SearchContractorByClient(user, fullName, email);
                if (query == null)
                {
                    return;
                }
                ListViewContractor.DataSource = query;   
            }
            else if(roleId==4)
            {
                var query = ContractorProfileLogic.SearchContractorByConsultant(user, fullName, email);
                if (query == null)
                {
                    return;
                }
                ListViewContractor.DataSource = query;  
            }
            ListViewContractor.DataBind();
        }

        protected void ButtonSearchClick(object sender, EventArgs e)
        {
            FilterContractor();
        }

    }
}
